#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <imp/imp_log.h>
#include <system/imp_alloc.h>

#define TAG "MemPool"
#define CHNNUM_MAX 32
struct Encoder_Pool
{
	int pools[CHNNUM_MAX]; //key chnNum, val poolId
};

static struct Encoder_Pool *g_pools;

int IMP_Encoder_SetPool(int chnNum, int poolID)
{
	if(chnNum < 0 || chnNum > CHNNUM_MAX) {
		IMP_LOG_ERR(TAG, "%s(%d):chnNum(%d) error\n", __func__, __LINE__, chnNum);
		return -1;
	}

	if(!IMP_MemPool_GetById(poolID)) {
		IMP_LOG_ERR(TAG, "%s(%d):POOL is not init\n", __func__, __LINE__);
		return -1;
	}

	if(!g_pools) {
		g_pools = (struct Encoder_Pool *)malloc(sizeof(struct Encoder_Pool));
		if(!g_pools) {
			IMP_LOG_ERR(TAG, "%s(%d):malloc pool error\n", __func__, __LINE__);
			return -1;
		}
		memset(g_pools, -1, sizeof(struct Encoder_Pool));
	}

	if(g_pools->pools[chnNum] >= 0) {
		IMP_LOG_ERR(TAG, "%s(%d):pools already set\n", __func__, __LINE__);
		return -1;
	}
	g_pools->pools[chnNum] = poolID;
	return 0;
}

int IMP_Encoder_GetPool(int chnNum)
{
	if(chnNum < 0 || chnNum > CHNNUM_MAX) {
		IMP_LOG_ERR(TAG, "%s(%d):chnNum(%d) error\n", __func__, __LINE__, chnNum);
		return -1;
	}

	if(!g_pools || g_pools->pools[chnNum] == -1) {
		IMP_LOG_ERR(TAG, "%s(%d):chnNum: %d not bind pool, If you don't use MemPool, don't pay attention!!\n", __func__, __LINE__, chnNum);
		return -1;
	}
	return g_pools->pools[chnNum];
}
